<?php

/**
 * Project: CrazyPoems
 * Author: Alexandru
 * Date: 20/10/2008
 * File: functions.misc.php
 */

function get_current_time() {
  return strftime("%Y-%m-%d %H:%M:%S", time());
}

function get_next_url() {
  $next_url = explode("?next=", get_full_url());
  if (isset($next_url[1])) {
    return $next_url[1];
  } else {
    return "";
  }
}

function get_full_url() {
  $sfu = 'http';
  if($_SERVER['HTTPS']=='on') {
    $sfu .=  's';
  }
  $sfu .=  '://';
  if($_SERVER['SERVER_PORT']!='80') $sfu .=  $_SERVER['HTTP_HOST'].':'.$_SERVER['SERVER_PORT'].$_SERVER['SCRIPT_NAME'];
  else
    $sfu .=  $_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
  if($_SERVER['QUERY_STRING']>' ') {
    $sfu .=  '?'.$_SERVER['QUERY_STRING'];
  }
  return $sfu;
}

function parseDate($date) {
  list($d, $t) = split(' ',$date);
  list($yr,$mon,$day) = split('-',$d);
  list($hr,$min,$sec) = split(':',$t);
  return date('d/m/Y', mktime($hr,$min,$sec,$mon,$day,$yr));
}

function checkSQL($sql) {
  // $sql = str_replace("'","''", $sql);
  // return $sql;
  return pSQL($sql);
}

/**
 * Sanitize data which will be injected into SQL query
 *
 * @param string $string SQL data which will be injected into SQL query
 * @param boolean $htmlOK Does data contain HTML code ? (optional)
 * @return string Sanitized data
 */
function pSQL($string, $htmlOK = false) {
  if (_MAGIC_QUOTES_GPC_)
    $string = stripslashes($string);
  if (!is_numeric($string)) {
    $string = _MYSQL_REAL_ESCAPE_STRING_ ? mysql_real_escape_string($string) : addslashes($string);
    if (!$htmlOK)
      $string = strip_tags(nl2br2($string));
  }

  return $string;
}

/**
 * Convert \n to <br />
 *
 * @param string $string String to transform
 * @return string New string
 */
function nl2br2($string) {
  return str_replace(array("\r\n", "\r", "\n"), '<br />', $string);
}

/* returns the current url without parameters you specify - $url = urlWithout("page,name,test"); */
function urlWithout($elementsToRemove) {
  $originalURL = "";
  $originalQueryString = "";
  $amp = "";
  $newQueryString = "";
  $q = "";
  $elementsToRemoveArray = array();
  $queryStringItems = array();
  $originalURL = $_SERVER['SCRIPT_NAME'];
  $originalQueryString = $_SERVER['QUERY_STRING'];
  if (strlen($originalQueryString) > 0) {
    if (strlen($elementsToRemove) > 0) {
      $queryStringItems = split("&", $originalQueryString);
      $elementsToRemoveArray = split(",", $elementsToRemove);
      for ($q = 0; $q < sizeof($queryStringItems); $q++) {
        if (!in_array(getItemName($queryStringItems[$q]), $elementsToRemoveArray)) {
          $newQueryString = $newQueryString . $amp . $queryStringItems[$q];
          $amp = "&amp;";
        }
      }
      if (strlen($newQueryString) > 0) {
        return $originalURL . "?" . $newQueryString . "&amp;";
      } else {
        return $originalURL . "?";
      }
    } else {
      return $originalURL . "?" . $originalQueryString;
    }
  } else {
    return $originalURL . "?";
  }
}

/* needed by urlWithout function */
function getItemName($queryItem) {
  $getName = array();
  if (strlen($queryItem) > 0) {
    $getName = split("=", $queryItem);
    return $getName[0];
  } else {
    return "";
  }
}

/* for javascript */
function enterToN($str) {
  if (strlen($str) > 0) {
    return str_replace("\r\n",'\n', $str);
  } else {
    return "";
  }
}

function get_clean_url_string($str) {
  $search = array(
          '/\s+/i',
          '/(-|_)(-|_)*/',
          '/[^a-z0-9_-]/i'
  );
  $replace = array(
          '-',
          '$1',
          ''
  );
  return preg_replace($search, $replace, trim($str));
}

function toSlug($string,$space="-") {

  if (function_exists('iconv')) {
    $string = @iconv('UTF-8', 'ASCII//TRANSLIT', $string);
  }

  $string = preg_replace("/[^a-zA-Z0-9 -]/", "", $string);
  $string = strtolower($string);
  $string = str_replace(" ", $space, $string);

  return $string;
}


function get_link($object, $parameters, $show_host = false) {
  $url = "";
  $host = "";
  if ($show_host)
    $host = 'http://'.$_SERVER['HTTP_HOST'];
  switch ($object) {
    case "poem":
      if (!USE_MOD_REWRITE) {
        $url = $host.BASE_URL."poem.php?id=".$parameters['id'];
      } else {
        $url = $host.BASE_URL."poezii/".toSlug($parameters['title'])."-".$parameters['id'];
      }
      break;
    default:
      $url = BASE_URL;
      break;
  }
  return $url;
}

function curl_get_file_contents($URL) {
  $c = curl_init();
  curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($c, CURLOPT_URL, $URL);
  $contents = curl_exec($c);
  curl_close($c);

  if ($contents) return $contents;
  else return FALSE;
}
?>
